<template>
	<xn-form-container :title="title" :width="400" :visible="visible" :destroy-on-close="true" @close="onClose">
		<a-row :gutter="24">
			<a-col :span="24">
				<a-spin :spinning="uploadLoading">
					<a-upload-dragger
						ref="uploadRef"
						:before-upload="handleBeforeUpload"
						v-model:file-list="fileList"
						:show-upload-list="true"
						:headers="headers"
						accept=".xlsx, .xls, .csv"
						:auto-upload="false"
						multiple
					>
						<p class="ant-upload-drag-icon">
							<inbox-outlined />
						</p>
						<p class="ant-upload-text">单击或拖动文件到此区域进行上传</p>
						<p class="ant-upload-hint">支持单个上传</p>
					</a-upload-dragger>
				</a-spin>
			</a-col>
			<a-col :span="24">
				<a-button type="link" @click="download">点击下载{{ title }}模版</a-button>
			</a-col>
			<a-divider />
			<a-col :span="24" v-if="isShowProject">
				<a-form-item label="所属项目">
					<ProjectSelect v-model:value="projectCode" />
				</a-form-item>
			</a-col>
		</a-row>
		<template #footer>
			<a-button class="xn-mr8" @click="onClose">关闭</a-button>
			<a-button
				type="primary"
				@click="onSubmit"
				:loading="submitBtnLoading"
				:disabled="fileList.length < 1 || (isEmpty(projectCode) && uploadType.value === 'case')"
				>保存
			</a-button>
		</template>
	</xn-form-container>
</template>
<script setup>
	import ProjectSelect from '@/components/Tjzy/Project/index.vue'
	import caseApi from '@/api/tjzy/case/caseApi'
	import tool from '@/utils/tool'
	import liaisonApi from '@/api/tjzy/liaison/liaisonApi'
	import allocateApi from '@/api/tjzy/allocate/allocateApi'
	import fileApi from '@/api/tjzy/file/fileApi'
	import downloadUtil from '@/utils/downloadUtil'
	import { isEmpty } from '@/utils/stringUtil'

	const emit = defineEmits({ successful: null })
	const headers = ref({
		token: tool.data.get('TOKEN')
	})
	const visible = ref(false)
	const uploadType = ref()
	const title = ref('')
	const uploadLoading = ref(false)
	const submitBtnLoading = ref(false)
	const fileList = ref([])
	const uploadRef = ref()
	const isShowProject = ref(false)
	const projectCode = ref()
	// 打开抽屉
	const openUpload = (type) => {
		uploadType.value = type
		visible.value = true
		if (type === 'case') {
			title.value = '案件导入'
			isShowProject.value = true
		}
		if (type === 'liaison') {
			title.value = '联系人导入'
		}
		if (type === 'excelAllocate') {
			title.value = '批量分案'
		}
		if (type === 'caseRetrieval') {
			title.value = '案件回收'
		}
		if (type === 'checkAllocate') {
			title.value = '案件分配'
		}
	}
	// 关闭抽屉
	const onClose = () => {
		visible.value = false
		isShowProject.value = false
		fileList.value = []
		emit('successful')
	}
	const handleBeforeUpload = () => {
		return false
	}
	const onSubmit = async () => {
		uploadLoading.value = true
		submitBtnLoading.value = true
		const fileData = new FormData()
		fileData.append('file', fileList.value[0].originFileObj)
		try {
			if (uploadType.value === 'case') {
				await caseApi.importCase(fileData, projectCode.value)
			}
			if (uploadType.value === 'liaison') {
				await liaisonApi.importLiaison(fileData)
			}
			if (uploadType.value === 'excelAllocate') {
				await allocateApi.importAllocate(fileData)
			}
			if (uploadType.value === 'upload') {
				await fileApi.uploadNoTimeSuffix(fileData)
			}
		} finally {
			emit('successful')
			visible.value = false
			uploadLoading.value = false
			submitBtnLoading.value = false
		}
	}

	const download = () => {
		let fileName = ''
		if (uploadType.value === 'case') {
			fileName = '案件导入模版.xlsx'
		}
		if (uploadType.value === 'liaison') {
			fileName = '联系人上传模版.xlsx'
		}
		if (uploadType.value === 'excelAllocate') {
			fileName = '批量分案模版.xlsx'
		}
		fileApi.downloadWithBucket('gzx-excel-template', fileName).then((res) => {
			downloadUtil.resultDownload(res)
		})
	}

	defineExpose({
		openUpload
	})
</script>
